package design.pattern.Lu16ObserverPattern;

import java.util.Observable;
import java.util.Observer;

/**
 * @program JavaBase
 * @description: 观察者模式 - 使用JDK提供的被观察者和观察者
 * @author: zhanglu
 * @create: 2020-04-10 10:26:00
 */
public class ObserverPattern04 {

    public static void main(String[] args) {
        LiSi04 liSi04 = new LiSi04();
        WangSi04 wangSi04 = new WangSi04();

        HanFeiZi04 hanFeiZi04 = new HanFeiZi04();

        //设置观察者
        hanFeiZi04.addObserver(liSi04);
        hanFeiZi04.addObserver(wangSi04);

        //被观察者 执行逻辑
        hanFeiZi04.haveBreakfast();
        hanFeiZi04.haveBreakfast();
        hanFeiZi04.haveBreakfast();
        hanFeiZi04.haveBreakfast();

    }

}

interface IHanFeiZi04{
    void haveBreakfast();
    void haveFun();
}

class HanFeiZi04 extends Observable implements IHanFeiZi02{

    @Override
    public void haveBreakfast() {
        System.out.println("韩非：吃饭了");
        this.setChanged();
        this.notifyObservers("韩非在吃饭");
    }

    @Override
    public void haveFun() {
        System.out.println("韩非：娱乐了");
        this.setChanged();
        this.notifyObservers("韩非在娱乐");
    }
}

class LiSi04 implements Observer {

    @Override
    public void update(Observable o, Object arg) {
        int count = o.countObservers();
        System.out.println("李斯监控：" + arg + "\t观察者数量：" + count);
    }
}

class WangSi04 implements Observer{

    @Override
    public void update(Observable o, Object arg) {
        int count = o.countObservers();
        System.out.println("王思监控：" + arg + "\t观察者数量：" + count);
    }
}
